package com.cheer.ba.view.activity;

import android.Manifest;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.presenter.impl.CardGroupPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.IViewController;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.XListView.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的订单
 *
 * @author HQ
 *         created at 2017/4/25 0025 14:08
 */
public class MyOilCardAty extends BaseCommAty implements XListView.IXListViewListener, AdapterView.OnItemClickListener {

    //没有数据显示界面
    @BindView(R.id.id_no_data_view)
    View mNoDataView;
    @BindView(R.id.id_pic_img)
    ImageView mImgNoPic;
    @BindView(R.id.id_search_info_txt)
    TextView mTxtNoData;
    @BindView(R.id.id_buy_card_btn)
    Button mBtnBuyCard;

    @BindView(R.id.id_oil_listv)
    XListView mListView;

    private CommonAdapter<MyOrderModel> mCardGroupAdp;

    //油卡列表
    public List<MyOrderModel> mCardGroups = new ArrayList<MyOrderModel>();

    public boolean isNoLogin;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_oil_card, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return !mCardGroups.isEmpty();
    }

    @Override
    public void initPresenter() {
        presenter = new CardGroupPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.mine_oil_order));
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        mCardGroupAdp = new CommonAdapter<MyOrderModel>(aty, mCardGroups, R.layout.adp_oil_cards) {
            @Override
            public void convert(ViewHolder helper, MyOrderModel item) {
                if (null != item) {
                    //立即使用
                    helper.setText(R.id.id_order_time_txt, StringUtils.stampToDate(item.getCreateTime() + "", "yyyy-MM-dd HH:mm:ss"));
                    helper.setText(R.id.id_oil_price_txt, Currency.returnDollar(Currency.RMB, item.getCostTrue(), 0));
                    String desc = item.getProductDesc() + "    " + StringUtils.removePoint(Currency.returnDollar(Currency.RMB, item.getCost(), 0).replace("元", "")) + "元    " + item.getOrderDesc();
                    helper.setText(R.id.id_oil_desc_txt, desc);
                    helper.getViewById(R.id.id_remind_txt).setVisibility(View.GONE);
                    if (item.getOrderState() == 4) {
                        helper.setImageResource(R.id.id_order_type_img, R.drawable.ic_already_paid);
                    } else if (item.getOrderState() == 5) {
                        helper.setImageResource(R.id.id_order_type_img, R.drawable.ic_completed);
                        helper.getViewById(R.id.id_remind_txt).setVisibility(View.VISIBLE);
                    }
                    helper.setText(R.id.id_order_num_txt, item.getOrderId());
                }
            }
        };
        mListView.setAdapter(mCardGroupAdp);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        setTitleRight("去加油", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入加油站
                checkOilPermiss();
            }
        });
        ((CardGroupPresenter) presenter).getMyOilCards(true, false, "5");
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.MY_OIL_CARDS)) {
                //我的油卡
                mListView.stopRefresh();
                mListView.stopLoadMore();
                List<MyOrderModel> myCards = (List<MyOrderModel>) t;
                if (!loadMore) {
                    mCardGroups.clear();
                }
                if (!myCards.isEmpty()) {
                    mCardGroups.addAll(myCards);
                    mCardGroupAdp.notifyDataSetChanged();
                } else {
                    hideFoot();
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //暂无数据
                        mNoDataView.setVisibility(View.VISIBLE);
                        mListView.setVisibility(View.GONE);
                        mImgNoPic.setBackgroundResource(R.drawable.ic_no_card);
                        mTxtNoData.setText("没有订单记录");
//                        mBtnBuyCard.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }

    @Override
    public void onLoadingStatus(IViewController mView, boolean isHandler, int loadingStatus, int flag, String string, boolean loadMore) {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        super.onLoadingStatus(mView, isHandler, loadingStatus, flag, string, loadMore);
    }

    @Override
    public void onRefresh() {
        if (mListView.getFooterViewsCount() <= 0) {
            mListView.addFooterView(mListView.mFooterView);
        }
        ((CardGroupPresenter) presenter).getMyOilCards(true, false, "5");
    }

    @Override
    public void onLoadMore() {
        ((CardGroupPresenter) presenter).getMyOilCards(true, true, "5");
    }

    private void hideFoot() {
        mListView.removeFooterView(mListView.getFooterView());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /**
     * 检测用户定位权限
     */
    private void checkOilPermiss() {
        String[] perms = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        if (!mayRequestPermission(perms)) {
            return;
        }
        getBaiduLocation();
    }

    /**
     * 获取地址
     */
    public void getBaiduLocation() {
        showActivity(aty, BaiduMapAty.class);
    }

    @Override
    public void requestPermissionResult(boolean allowPermission) {
        if (allowPermission) {
            getBaiduLocation();
        }
    }
}
